package servlet;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class receives_Add extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public receives_Add() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    //添加优惠券领取信息
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        PrintWriter out = response.getWriter();

        conn cn = new conn();


        String f = "0";

        String mid = request.getParameter("mid");
        String cid = request.getParameter("cid");


        if (cn.exist("select * from receives where cid=" + cid + " and mid=" + mid)) {
            f = "1";
        } else {

            String sql = "insert into receives(mid,cid,rtime,flag) values(" + mid + "," + cid + ",now(),'未使用')";
           


            int i = cn.update(sql);

            cn.update("update coupon set cquan=cquan-1 where cid=" + cid);
        }


        JSONArray jsonarray = new JSONArray();
        JSONObject jsonobj = new JSONObject();
        jsonobj.put("jieguo", f);
        jsonarray.add(jsonobj);
        out = response.getWriter();
        out.println(jsonarray);


        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

